﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.Web.Caching;
using GongXianDCUI.Models;
using GongXianDCUI.BLL;
using Webdiyer.WebControls.Mvc;

namespace GongXianDCUI.Controllers
{
    public class HomeController : Controller
    {


        [OutputCache(Duration = 3600)]
        public ActionResult Index()
        {
            //获取首页公司新闻
            ArticleBLL articBll = new ArticleBLL();
            PagedList<Article> listPaing = articBll.GetArticlePage(1, 2, 300, 1);
            List<Article> list = listPaing.ToList();
            ViewBag.NewsList = list;
            //获取第一条公司新闻展示在首页
            long firstArticleId = 0;
            Article model = new Article();
            if (list.Count > 0)
            {
                firstArticleId = list.First().articleId;
                model = articBll.GetModel(firstArticleId);
                if (model != null)
                {
                    model.content = GongXianDCUI.CommonLib.HtmlHandle.DeleteHTML(model.content);
                    int length = model.content.Length > 70 ? 70 : model.content.Length;
                    if (model.content.Length > 70)
                    {
                        model.content = model.content.Substring(0, length) + "...";
                    }
                    else
                    {
                        model.content = model.content.Substring(0, length);
                    }
                }
            }
            ViewBag.FirstNews = model;
            //tab列表
            LayoutArticleBLL BLL = new LayoutArticleBLL();
            FunctionsBLL funBLL =new  FunctionsBLL();
            //获取首页布局数据
            ViewBag.LayoutArticleList = BLL.GetLayoutArticle();
            //获取电子公告默认菜单id为1000
            ViewBag.Bulletin = BLL.getBulletin(1000);
            //获取通知公告
            ViewBag.TongZhi = BLL.getBulletin(1001);
            //顶部大图片1401
            List<Article> TopImages = articBll.GetArticleList(1401);
            //底部轮播小图片1402
            List<Article> buttomImages = articBll.GetArticleList(1402);
            //公司简介200
            List<Article> ArticleTop1 = articBll.GetArticleList(200);
            //读取企业文化子菜单7
            ViewBag.qiYeWenHua = funBLL.GetFuncitionsList(7);
            ViewBag.ArticleTop1 = ArticleTop1;
            ViewBag.topImage = TopImages;
            ViewBag.buttomImages = buttomImages;
            return View();
        }

        [ChildActionOnly]
        //加载头部菜单
        public PartialViewResult PartialHeadMenu()
        {
            List<Funcitions> list = GetFunctionList(HttpContext);
            return PartialView(list);
        }

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public List<Funcitions> GetFunctionList(HttpContextBase context)
        {
            List<Funcitions> list = null;
            System.Web.Caching.Cache cacheData = context.Cache;
            list = cacheData.Get("functions") as List<Funcitions>;
            if (list == null)
            {
                FunctionsBLL functionsBll = new FunctionsBLL();
                list = functionsBll.GetFunctionList();
                //缓存数据
                cacheData.Insert("functions", list, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(60));
            }
            return list;
        }

        

    }
}